package com.wythe.mall.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatRatingBar;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.wythe.mall.R;
import com.wythe.mall.activity.EvaluateActivity;
import com.wythe.mall.model.OrderBean;
import com.wythe.mall.view.NoScrollGridView;

import java.util.ArrayList;
import java.util.List;

import top.yokey.base.bean.OrderEvaluateBean;

import static com.wythe.mall.activity.EvaluateActivity.PICK_IMAGE;

public class OrderEvaluateListAdapter extends RecyclerView.Adapter<OrderEvaluateListAdapter.ViewHolder> implements PublishPicAndMessAdapter.OnImagesChangedCallback {

    private final List<OrderBean.ItemVoBean> arrayList;
    private OnItemClickListener onItemClickListener;
    private ArrayList<String> mImages;
    private OnImagesChangedCallback mCallBack;
    private Context mContext;
    private List<PublishPicAndMessAdapter> mLists;

    public OrderEvaluateListAdapter(Context context,List<OrderBean.ItemVoBean> arrayList) {
        this.arrayList = arrayList;
        mLists = new ArrayList<>();
        this.onItemClickListener = null;
        mContext = context;
    }

    @Override
    public int getItemCount() {

        return arrayList.size();

    }

    @Override
    public void onBindViewHolder(final ViewHolder holder, final int position) {

        final int positionInt = position;
        OrderBean.ItemVoBean itemVoBean = arrayList.get(positionInt);
        Glide.with(mContext)
                .load(itemVoBean.getGoodsVo().getIcon())
                .dontAnimate()
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(holder.mainImageView);
        holder.nameTextView.setText(itemVoBean.getGoodsVo().getTitle());
        final PublishPicAndMessAdapter mTraileMessageImageAdapter = new PublishPicAndMessAdapter(mContext,this);
        mLists.add(mTraileMessageImageAdapter);
        holder.mGridImage.setAdapter(mTraileMessageImageAdapter);
        holder.evaluateEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (onItemClickListener != null) {
                    onItemClickListener.onTextChange(positionInt,holder.evaluateEditText,holder.evaluateEditText.getText().toString());
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        holder.mGridImage.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                String url = mTraileMessageImageAdapter.getItem(position);
                if (url == null) {
                    mTraileMessageImageAdapter.showImageSelector((Activity) mContext, EvaluateActivity.PICK_IMAGE);
                }

                if (onItemClickListener != null) {
                    onItemClickListener.onClick(positionInt,mTraileMessageImageAdapter);
                }
            }
        });
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup group, int viewType) {
        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_list_order_evaluate, group, false);
        return new ViewHolder(view);
    }

    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }

    public List<PublishPicAndMessAdapter> getAdapter() {
        return mLists;
    }

    @Override
    public void onImagesChanged() {

    }

    public interface OnItemClickListener {

        void onClick(int position, PublishPicAndMessAdapter adapter);

        void onTextChange(int position,AppCompatEditText editText,String text);

//        void onClickImage(int position, int positionImage, OrderEvaluateBean.OrderGoodsBean bean);

    }

    class ViewHolder extends RecyclerView.ViewHolder {

        private RelativeLayout mainRelativeLayout;
        private AppCompatImageView mainImageView;
        private AppCompatTextView nameTextView;
        private AppCompatRatingBar mainRatingBar;
        private AppCompatEditText evaluateEditText;
        private NoScrollGridView mGridImage;
        private ViewHolder(View view) {
            super(view);
            mainRelativeLayout = view.findViewById(R.id.mainRelativeLayout);
            mainImageView = view.findViewById(R.id.mainImageView);
            nameTextView = view.findViewById(R.id.nameTextView);
            mainRatingBar = view.findViewById(R.id.mainRatingBar);
            evaluateEditText = view.findViewById(R.id.evaluateEditText);
            mGridImage = view.findViewById(R.id.grid_image);
        }

    }

    /**
     * 图片变化回调
     */
    public interface OnImagesChangedCallback {
        /**
         * 图片更改
         */
        void onImagesChanged();
    }

}
